package com.watson.view;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class CenterCircleDrawable extends Drawable {
    private Paint paint;
    private int width;
    private int height;

    public CenterCircleDrawable() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        // 设置圆形透明中心的画笔
        paint.setColor(Color.TRANSPARENT);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        // 设置边缘的画笔
        Paint borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        borderPaint.setColor(Color.BLACK);
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(5f); // 可以根据需要调整边框宽度
        paint.setStrokeJoin(Paint.Join.ROUND);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        // 设置圆的大小为最小边的一半
        int min = Math.min(width, height);
        int radius = min / 2;
        // 圆的中心位于drawable的中心
        int x = width / 2;
        int y = height / 2;
        // 先画圆形透明中心
        canvas.drawCircle(x, y, radius, paint);
        // 再画边缘
        canvas.drawCircle(x, y, radius, paint);
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        paint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        width = bounds.width();
        height = bounds.height();
    }
}
